package Servlet;

import DAO.MessageDAO;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

@WebServlet(name= "enrollServlet",value = "/enrollServlet")
public class enrollServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setCharacterEncoding("UTF-8");
        String UserName= request.getParameter("UserName");
        String password=request.getParameter("password");
        String SurePassword=request.getParameter("SurePassword");
        MessageDAO messageDAO=new MessageDAO();
        System.out.println(password);
        System.out.println(SurePassword);
        if(UserName!=""&&password!=""&&SurePassword!=""){
            String str="true";
            if(!Objects.equals(password,SurePassword)){
                response.getWriter().print("密码不一致，请重新输入！");
            }else{
                if(messageDAO.enrollServlet(UserName,password)==true){
                    response.getWriter().write(str);
                }
                else {
                    response.getWriter().print("用户名已存在！");
                }
            }

        }
         else if(SurePassword==""){
            response.getWriter().write("请再次输入密码！");
        }
        else {
            response.getWriter().write("用户名和密码不能为空！");
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}
